package com.gqc.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gqc.bean.Cart;
import com.gqc.bean.Order;
import com.gqc.bean.User;
import com.gqc.service.OrderService;
import com.gqc.service.UserService;

import net.sf.json.JSONObject;

@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("/addOrder")
	public String addOrder(String user,String business,String price) throws ParseException {
		System.out.println("**********price");
		System.out.println(price);
		Integer user1 = Integer.valueOf(user);
		Integer business1 = Integer.valueOf(business);
		Integer price1 = Integer.valueOf(price);
		Order order = new Order();
		order.setUser(user1);
		order.setBusiness(business1);
		order.setPrice(price1);
		order.setStatus(1);
		System.out.println(order.getPrice());
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sdf.format(date);
		order.setDate(date);
		
		System.out.println(date);
		System.out.println("date************");
		System.out.println(order.getDate());
		User user2 = userService.queryById(user1);
		order.setTel(user2.getTel());
		order.setAddress(user2.getAddress());
		//order.setStatus(status);
		orderService.addOrder(order);
		return "user";
	}
	
	//商家查询订单
		@RequestMapping("/findOrderByBusiness")
		@ResponseBody
	    public JSONObject  findOrderByBusiness(HttpServletRequest request , @RequestParam("page")String page,@RequestParam("limit")String limit,@RequestParam("id") int id){
	    	List<Order> orderList = orderService.findOrderByBusiness(id);
	    	JSONObject json = new JSONObject();
	    	json.put("msg", "success");
	    	json.put("code", "0");
	        json.put("data", orderList);
	        request.getSession().setAttribute("orderList", orderList);
	        json.put("count", orderList.size());
	        return json;
	    }
	    //送货
		@RequestMapping("/sendFood")
		@ResponseBody
	    public boolean sendFood(@RequestBody int id) {
	    	orderService.sendFood(id);
	    	return true;
	    }
	    //接受订单
		@RequestMapping("/getOrder")
		@ResponseBody
	    public boolean getOrder(int id) {
			orderService.getOrder(id);
			return true;
	    }
		//接受订单
				@RequestMapping(value = "/findOrder",method = RequestMethod.POST)
				@ResponseBody
			    public boolean findOrder(@RequestParam("id") int id) {
					orderService.getOrder(id);
					return true;
			    }

		//客户查询订单
				@RequestMapping("/findOrderByUser")
				@ResponseBody
			    public List<Order> findOrderByUser(HttpServletRequest request,@RequestBody int id){
					return orderService.findOrderByUser(id);
				}
			 

}
